package com.onstar.cn.common.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.onstar.cn.common.jsBridge.BridgeWebView;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * Image processing tools
 *
 * @author 7828wc
 */
public final class BitmapUtil {

    /**
     * imageView released
     *
     * @param imageView
     */
    public final static void recycleImageView(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                imageView.setBackground(null);
            } else {
                imageView.setBackgroundDrawable(null);
            }
        }
    }

    /**
     * button released
     *
     * @param button
     */
    public final static void recycleButton(Button button) {
        if (button != null) {
            Drawable drawable = button.getBackground();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            button.setBackground(null);
        }
    }

    public final static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 回收ImageView占用的图像内存;
     * @param view
     */
    public static void recycleImageViewWithBitMap(View view){
        if(view==null) return;
        if(view instanceof ImageView){
            Drawable drawable=((ImageView) view).getDrawable();
            if(drawable instanceof BitmapDrawable){
                Bitmap bmp = ((BitmapDrawable)drawable).getBitmap();
                if (bmp != null && !bmp.isRecycled()){
                    ((ImageView) view).setImageBitmap(null);
                    bmp.recycle();
                    bmp=null;
                }
            }
        }
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId)
    {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }


    /**
     * WebVie内容截屏
     */
    public static Bitmap webViewShot(BridgeWebView mWebView) {
        float scale = mWebView.getScale();
        int height = (int) (mWebView.getContentHeight() * scale + 0.5);
        Bitmap bitmap = Bitmap.createBitmap(mWebView.getWidth(), height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        mWebView.draw(canvas);
        return bitmap;
    }
}
